package com.we823.project.controller.report;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.project.common.web.status.LogType;
import com.we823.project.controller.AbstractBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.sys.LogModel;
import com.we823.project.repository.service.sys.LogService;
import com.we823.utils.web.RequestUtil;

@Controller
@RequestMapping(WebConstants.REPORT_LOG)
public class LogController extends AbstractBaseController<LogModel> {

	private LogService logService;
	
	private static String baseUrl = WebConstants.REPORT_LOG_TEMPLATE;
	@Autowired
	public LogController(LogService logService) {
		super(logService);
		super.baseUrl = baseUrl;
		super.entityName = "log";
		this.logService=logService;
		
		setResourceIdentity("report:log");
	}

	@Override
	public Page<LogModel> getData(PageRequest pageRequest,
			HttpServletRequest request, Model model, boolean pageable) {
		Page<LogModel> logPage = null;
		if(pageable) {
			logPage = logService.findAll(pageRequest);
		}else {
			logPage = new PageImpl<LogModel>(logService.findAll());
		}
		return logPage;
	}

	protected org.springframework.data.domain.Sort getSort(HttpServletRequest request) {
		return new Sort(Direction.DESC,"addtime");
	}
	
	@RequestMapping("addLog")
	@ResponseBody
	public Model addProcessLog(HttpServletRequest request,Model model) {
		String message = RequestUtil.getValueByParam(request, "m");
		String code = RequestUtil.getValueByParam(request, "c");
		logService.saveLog(message, code, null);
		model.addAttribute("message","保存记录");
		return model;
	}
}
